package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.common.query.StrategyQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;


    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<StrategyDetail> query(StrategyQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.query4List(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //通过攻略目录获取目的地id
        Long destId = strategyCatalogMapper.getDestIdById(strategyDetail.getCatalog().getId());
        Destination destination = new Destination();
        destination.setId(destId);
        //设置目的地
        strategyDetail.setDest(destination);
        //设置简介
        strategyDetail.setSummary("AI智能识别...");
        if (strategyDetail.getId() == null) {
            //保存
            //设置创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            //保存内容
            StrategyContent content = strategyDetail.getStrategyContent();
            content.setId(strategyDetail.getId());
            strategyContentMapper.insert(content);
        } else {
            //编辑
            //打破关系
            strategyDetailMapper.deleteRelation(strategyDetail.getId());
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //修改内容
            StrategyContent content = strategyDetail.getStrategyContent();
            content.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(content);
        }

        //设置标签
        if (tags != null && !"".equals(tags.trim())) {
            String[] tagNames = tags.split(",");
            for (String tagName : tagNames) {
                //判断是否存在
                StrategyTag strategyTag = strategyTagMapper.selectByName(tagName);
                //不存在才保存
                if (strategyTag == null) {
                    strategyTag = new StrategyTag();
                    strategyTag.setName(tagName);
                    strategyTagMapper.insert(strategyTag);
                }
                //维护关系
                strategyTagMapper.insertRelation(strategyTag.getId(), strategyDetail.getId());
            }
        }

    }

    @Override
    public List<StrategyDetail> selectByCatalogId(Long id) {
        return strategyDetailMapper.selectByCatalogId(id);
    }

    @Override
    public List<StrategyDetail> selectViewNumTop3(Long destId) {
        return strategyDetailMapper.selectViewNumTop3(destId);
    }

    @Override
    public List<StrategyDetail> selectByDestIdTop3(Long id) {
        return strategyDetailMapper.selectByDestIdTop3(id);
    }

    @Override
    public List<StrategyDetail> listAll() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void updateStats(StrategyDetail detail) {
        strategyDetailMapper.updateStats(detail);
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    /**
     * 保存用户和攻略的关系
     *
     * @param favorId
     * @param userId
     */
    public void saveUserRelation(Long favorId, Long userId) {
        strategyDetailMapper.insertUserRelation(favorId, userId);
    }

    /**
     * 判断是否已存在收藏
     *
     * @param favorId
     * @param userId
     * @return true 该用户没有收藏这条攻略
     */
    public boolean isUserRelation(Long favorId, Long userId) {
        List<Long> ids = strategyDetailMapper.selectByUserId(userId);
        for (Long id : ids) {
            if (id == favorId) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取用户的所有收藏游记
     *
     * @param userId
     * @return
     */
    public List<StrategyDetail> getByUserId(Long userId) {
        return strategyDetailMapper.selectAllByUserId(userId);
    }

    /**
     * 删除用户指定的收藏游记
     *
     * @param id
     * @return 成功删除关系
     */
    public boolean deleteCollection(Long id) {
        try {
            strategyDetailMapper.deleteRelationByUserId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
